#include "event_app.h"
#include "battery_board.h"

#if ((_BOARD_TYPE_ != _V32_B_BOARD_) && ((_BOARD_TYPE_ & _DA01_BOARD_) != _DA01_BOARD_) && ((_BOARD_TYPE_ & _CR01_BU_) != _CR01_BU_))
/**
 * @brief 配置采样管脚为 ADC 功能
 *
 */
static void BatteryConfiguringDetectionPinFunctions(void);

/**
 * @brief 配置电压采样管脚功能
 *
 */
static void BatteryDetectionEnableConfiguration(void);

/**
 * @brief 设置检测电压使能管脚为高电平
 *
 */
static void BatterySetDetectionEnableHightLevel(void);

/**
 * @brief 设置检测电压使能管脚为低电平
 *
 */
static void BatterySetDetectionEnableLowLevel(void);

static signed short AdcRoughCalibValue = 0;

static void BatteryConfiguringDetectionPinFunctions(void)
{
  GPIOA_ModeCfg(BATTERY_VOLTAGE_DETECTION_PIN, GPIO_ModeIN_Floating);

  ADC_ExtSingleChSampInit(SampleFreq_8, ADC_PGA_0);
  AdcRoughCalibValue = ADC_DataCalib_Rough();
  ADC_ChannelCfg(BATTERY_VOLTAGE_DETECTION_CHANNEL);

}

static void BatteryDetectionEnableConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ModeCfg(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN, GPIO_ModeOut_PP_5mA);
#endif

  BatterySetDetectionEnableLowLevel();
}

static void BatterySetDetectionEnableHightLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_SetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_SetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);
#endif
}

static void BatterySetDetectionEnableLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOA_ResetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ResetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(BATTERY_VOLTAGE_DETECTION_ENABLE_PIN);
#endif
}

void BatteryDelayUs(uint16_t Us)
{
  DelayUs(Us);
}

void BatteryDelayMs(uint16_t Ms)
{
  DelayMs(Ms);
}

void BatteryEnableDetection(void)
{
  ADC_ExtSingleChSampInit(SampleFreq_8, ADC_PGA_0);
  AdcRoughCalibValue = ADC_DataCalib_Rough();
  ADC_ChannelCfg(BATTERY_VOLTAGE_DETECTION_CHANNEL);

  BatterySetDetectionEnableHightLevel();
}

void BatteryDisableDetection(void)
{
  BatterySetDetectionEnableLowLevel();
}

uint32_t BatteryGetInputVoltage(void)
{
  AdcRoughCalibValue = ADC_DataCalib_Rough();
  uint16_t SamplingData = ADC_ExcutSingleConver() + AdcRoughCalibValue;

  return (uint32_t)(((SamplingData * 1000) / 2048.f) * 1.05f);
}

void BatteryBoardInitialisation(void)
{
  BatteryDetectionEnableConfiguration();
  BatteryConfiguringDetectionPinFunctions();
}
#endif
